package com.lc.user.express.ordering;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.utils.GetToast;


/**
 * Created by Administrator on 2016/3/3. 给司机捎句话
 */
public class MessageToDiverActivity extends BaseSecondActivity {
    private EditText et_message;//输入信息
    private TextView tv_count;//字数
    private TextView tv_public;//确认
    private int cursorPos;
    //输入表情前EditText中的文本
    private String inputAfterText;
    //是否重置了EditText的内容
    public boolean resetText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_to_diver);
        setTitle("给司机捎句话");
        et_message = (EditText) findViewById(R.id.et_message);
        tv_count = (TextView) findViewById(R.id.tv_count);
        tv_public = (TextView) findViewById(R.id.tv_public);

        String str = getIntent().getStringExtra("message");
        et_message.setText(str);
        if (!TextUtils.isEmpty(str)) {
            et_message.setSelection(str.length());
            tv_count.setText(str.length() + "/60");
        }


        tv_public.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(et_message.getText().toString().trim())) {
                    GetToast.useString(cnt, "请输入内容");
                    return;
                }
                if (OrderInfoCompleteActivity.onMessageToDiver != null) {
                    OrderInfoCompleteActivity.onMessageToDiver.onMessage(et_message.getText().toString().trim());
                }
                finish();
            }
        });
        et_message.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count >= 2) {//表情符号的字符长度最小为2
                    CharSequence input = s.subSequence(cursorPos, cursorPos + count);
                    if (containsEmoji(input.toString())) {
                        resetText = true;
                        Toast.makeText(getApplicationContext(), "不支持输入表情符号", Toast.LENGTH_SHORT).show();
                        //是表情符号就将文本还原为输入表情符号之前的内容
                        et_message.setText(inputAfterText);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_count.setText(s.toString().length() + "/60");
            }
        });

    }




    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     * @return
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }
}
